package org.lennon.demo.ddd.ticket.aggregate;

public class Ticket {

    public Long id;

    public Long userId;

    public Integer amount;

    public Ticket(Long id, Long userId, Integer amount) {
        this.id = id;
        this.userId = userId;
        this.amount = amount;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    /**
     * 加券
     *
     * @param amount
     */
    public void giveOut(Integer amount) {
        if(amount < 1) {
            throw  new RuntimeException("加券数量不得小于1");
        }
        this.amount += amount;
    }

    /**
     * 扣券
     *
     * @param amount
     */
    public void reduce(Integer amount) {
        if(amount < 1) {
            throw new RuntimeException("扣券数量不得小于0");
        }
        if(amount > this.amount) {
            throw new RuntimeException("扣券数量不得超过已有数量");
        }
        this.amount -= amount;
    }
}
